﻿using System.Xml;

namespace QDT.Payment.AllInPay.Api
{
    /// <summary>
    /// 响应报文基类
    /// </summary>
    public class Response : CustomPackage
    {
        protected string
        m_strTrxCode,
        m_strVersion,
        m_strDataType,
        m_strReqSn,
        m_strRetCode,
        m_strErrMsg,
        m_strSignedMsg,
        trans_ret_code,
        trans_ret_msg;

        internal Response()
        {
        }
        public string Trans_ret_code
        {
            get{return trans_ret_code;}
            set{trans_ret_code = value;}
        }
        public string Trans_ret_msg
        {
            get { return trans_ret_msg; }
            set { trans_ret_msg = value; }
        }
          
        /// <summary>
        /// 交易代码
        /// </summary>
        public string TrxCode
        {
            get { return m_strTrxCode; }
        }
        /// <summary>
        /// 报文版本
        /// </summary>
        public string Version
        {
            get { return m_strVersion; }
        }
        internal string DataType
        {
            get { return m_strDataType; }
        }
        /// <summary>
        /// 流水号
        /// </summary>
        public string ReqSn
        {
            get { return m_strReqSn; }
        }
        /// <summary>
        /// 返回码
        /// </summary>
        public string RetCode
        {
            get { return m_strRetCode; }
            set
            { m_strRetCode = value; }
        }
        /// <summary>
        /// 错误文本
        /// </summary>
        public string ErrMsg
        {
            get { return m_strErrMsg; }
            set
            { m_strErrMsg = value; }
        }
        internal string SignedMsg
        {
            get { return m_strSignedMsg; }
        }

        internal virtual void ParseXML(XmlDocument xmlDoc)
        {
            m_package = xmlDoc;
            DoParseXML();
        }

        internal virtual void ParseXML(string xmlText)
        {
            m_package = new XmlDocument();
            m_package.LoadXml(xmlText);

            DoParseXML();
        }

        private void DoParseXML()
        {
            m_elInfo = (XmlElement)m_package.SelectSingleNode("/" + InternalConstants.RootNode + "/INFO");
            m_elBody = (XmlElement)m_package.SelectSingleNode("/" + InternalConstants.RootNode + "/BODY");
            m_elTransRet = (XmlElement)m_package.SelectSingleNode("/" + InternalConstants.RootNode + "/TRANSRET");



            m_strTrxCode = GetText("/" + InternalConstants.RootNode + "/INFO/TRX_CODE/node()");
            m_strVersion = GetText("/" + InternalConstants.RootNode + "/INFO/VERSION/node()");
            m_strDataType = GetText("/" + InternalConstants.RootNode + "/INFO/DATA_TYPE/node()");
            m_strReqSn = GetText("/" + InternalConstants.RootNode + "/INFO/REG_SN/node()");
            m_strRetCode = GetText("/" + InternalConstants.RootNode + "/INFO/RET_CODE/node()");
            m_strErrMsg = GetText("/" + InternalConstants.RootNode + "/INFO/ERR_MSG/node()");
            m_strSignedMsg = GetText("/" + InternalConstants.RootNode + "/INFO/SIGNED_MSG/node()");
        }

        protected string GetText(string xpath)
        {
            return GetText(m_package, xpath);
        }

        protected string GetText(XmlNode root, string xpath)
        {
            XmlNode node = root.SelectSingleNode(xpath);
            if (node == null || node.NodeType != XmlNodeType.Text)
                return "";
            else
                return ((XmlText)node).Value;
        }
    }
}
